package user

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"github.com/satori/go.uuid"
	"talent.com/agency_im/conf/g"
)

// 用户基本管理
func GetOrCreateUser(user *User) (*User, error) {
	var (
		existUser User
		err       error
	)

	err = g.ORM.Where(&User{PlatformID: user.PlatformID, ThirdUserID: user.ThirdUserID}).First(&existUser).Error
	if err == nil {
		existUser.Nickname = user.Nickname
		existUser.PortraitUri = user.PortraitUri
		existUser.LeavingTime = user.LeavingTime

		g.ORM.Save(&existUser)
		return &existUser, nil
	} else {
		g.ORM.Create(&user)
	}
	return user, nil
}

func GetUser(ID uint64) (*User, error) {
	var (
		u   User
		err error
	)
	err = g.ORM.Where("id = ?", ID).First(&u).Error
	return &u, err
}

func GetUserByThirdInfo(platformID uint64, thirdUserID uint64) (*User, error) {
	var (
		u   User
		err error
	)
	err = g.ORM.Where("platform_id = ? and third_user_id = ?", platformID, thirdUserID).First(&u).Error
	return &u, err
}

func UpdateUserInfo(u *User) error {
	var (
		uu  *User
		err error
	)
	if uu, err = GetUser(u.ID); err != nil {
		return err
	}
	uu.Nickname = u.Nickname
	uu.PortraitUri = u.PortraitUri
	uu.Status = u.Status
	uu.LeavingTime = u.LeavingTime
	if err = g.ORM.Save(uu).Error; err != nil {
		return err
	}
	return nil
}

// 登录信息管理
func AddAuthToken(userID uint64) (token *AuthToken, err error) {
	token = &AuthToken{UserID: userID, Token: fmt.Sprintf("%v", uuid.NewV1())}
	if err := g.ORM.FirstOrCreate(token, AuthToken{UserID: userID}).Error; err != nil {
		return nil, err
	}
	return
}

func GetAuthToken(token string) (authToken *AuthToken, err error) {
	authToken = &AuthToken{}
	err = g.ORM.Where("token = ?", token).First(authToken).Error
	return
}

// 黑名单管理
//func AddBlacklist(sourceID uint64, targetID uint64) error {
//	blacklist := &Blacklist{SourceID: sourceID, TargetID: targetID}
//	if err := g.ORM.Save(blacklist).Error; err != nil {
//		return err
//	}
//	return nil
//}
//
//func RemoveBlacklist(sourceID uint64, targetID uint64) error {
//	if blacklist, err := GetBlacklist(sourceID, targetID); err != nil {
//		return err
//	} else {
//		g.ORM.Delete(blacklist)
//		return nil
//	}
//}
//
//func GetBlacklist(sourceID uint64, targetID uint64) (*Blacklist, error) {
//	var (
//		blacklist Blacklist
//		err       error
//	)
//	if err = g.ORM.Where("source_id = ? and target_id = ?", sourceID, targetID).First(&blacklist).Error; err != nil {
//		if gorm.IsRecordNotFoundError(err) {
//			return nil, nil
//		}
//		return nil, err
//	} else {
//		return &blacklist, nil
//	}
//}

// 黑名单管理
func AddRemark(sourceID uint64, targetID uint64, name string) error {
	remark := &Remark{SourceID: sourceID, TargetID: targetID, Name: name}
	if err := g.ORM.Save(remark).Error; err != nil {
		return err
	}
	return nil
}

func GetRemark(sourceID uint64, targetID uint64) (*Remark, error) {
	var (
		remark Remark
		err    error
	)
	if err = g.ORM.Where("source_id = ? and target_id = ?", sourceID, targetID).First(&remark).Error; err != nil {
		if gorm.IsRecordNotFoundError(err) {
			return nil, nil
		}
		return nil, err
	} else {
		return &remark, nil
	}
}

func CreateOrUpdateRemark(r *Remark) error {
	var (
		remark *Remark
		err    error
	)
	if remark, err = GetRemark(r.SourceID, r.TargetID); err != nil {
		return err
	}

	if remark == nil {
		remark = r
	} else {
		remark.Name = r.Name
	}

	if err = g.ORM.Save(remark).Error; err != nil {
		return err
	}
	return nil
}
